package com.cater.talents.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.common.UserContext;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.User;
import com.cater.sys.service.UserService;
import com.cater.talents.entity.WorkSearch;
import com.cater.talents.service.WorkSearchServiceImpl;
import com.cater.utils.InputExcelUtil;
import com.cater.utils.StringUitl;

/**
 * 描述：工作记录表控制层
 * 
 * @author yong
 * @date 2019/1/5
 */
@Controller
@RequestMapping("/workSearch")
public class WorkSearchController {

	@Autowired
	private WorkSearchServiceImpl service;
	@Autowired
	private UserService userService;// 用户

	/**
	 * @工作记录表管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model) {
		List<User> users = userService.getAll();
		model.addAttribute("users", users);
		return "talents/workSearch/list";
	}

	/**
	 * @工作记录表管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询工作记录表分页数据")
	public PageResult<WorkSearch> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<WorkSearch> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("workSearch:add")
	public String addView() {
		return "talents/workSearch/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("workSearch:add")
	@ResponseBody
	@SystemControllerLog(description = "添加工作记录表")
	public JSONObject add(WorkSearch t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("workSearch:edit")
	public String editView(String id, Model model, CommonQuery query) {
		WorkSearch t = service.getById(id);
		model.addAttribute("workSearch", t);
		return "talents/workSearch/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("workSearch:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改工作记录表")
	public JSONObject update(WorkSearch t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("workSearch:delete")
	@SystemControllerLog(description = "删除工作记录表")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("workSearch:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		WorkSearch t = service.getById(id);
		model.addAttribute("workSearch", t);
		return "talents/workSearch/detail";
	}

	/**
	 * 导入工作记录
	 * 
	 * @param t
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "inputExl")
	@RequiresPermissions("workSearch:input")
	@ResponseBody
	@SystemControllerLog(description = "导入工作记录表")
	public JSONObject inputExl(WorkSearch t, CommonQuery query, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/");
		try {
			String path = request.getServletContext().getRealPath("/static/upload/excel/") + sdf.format(new Date());
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multipartRequest.getFile("file");
			String fileName = file.getOriginalFilename();
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			file.transferTo(new File(path + fileName));
			List<List<Object>> dataList = InputExcelUtil.importExcel(new File(path + fileName));
			List<WorkSearch> workSearchs = new ArrayList<WorkSearch>();
			for (int i = 1; i < dataList.size(); i++) {
				List<Object> temps = dataList.get(i);
				if (temps.size() > 1) {
					WorkSearch wSearch = new WorkSearch();
					wSearch.setIsChange(temps.get(0).toString());
					wSearch.setCompanyName(temps.get(1).toString());
					wSearch.setArea(temps.get(2).toString());
					wSearch.setCity(temps.get(3).toString());
					wSearch.setPostition(temps.get(4).toString());
					wSearch.setTrueName(temps.get(5).toString());
					wSearch.setSex(temps.get(6).toString());
					wSearch.setAge(temps.get(7).toString());
					wSearch.setContact(temps.get(8).toString());
					wSearch.setEmail(temps.get(9).toString());
					wSearch.setRemark(temps.get(10).toString());
					if (temps.size() > 11) {
						wSearch.setRecords(temps.get(11).toString());
					}
					wSearch.setCreateUserId(UserContext.getId());
					wSearch.setCreateDate(StringUitl.getDate());
					wSearch.setId(UUID.randomUUID().toString().replaceAll("-", ""));
					workSearchs.add(wSearch);
					// service.saves(wSearch);
				}
			}
			service.insertList(workSearchs);
			json.put("code", 200);
			json.put("message", "导入成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "导入失败");
		}
		return json;
	}

	@RequestMapping(value = "addResume")
	@RequiresPermissions("workSearch:resume")
	public String addResume(String id, Model model, CommonQuery query) {
		WorkSearch t = service.getById(id);
		model.addAttribute("workSearch", t);
		model.addAttribute("id", UUID.randomUUID().toString().replaceAll("-", ""));
		return "talents/workSearch/add_resume";
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteList")
	@RequiresPermissions("workSearch:deleteList")
	public JSONObject deleteList(String ids) {
		JSONObject json = new JSONObject();
		try {
			if (StringUtils.isNotEmpty(ids)) {
				String[] tids = ids.split(",");
				service.deleteList(tids);
				json.put("code", 200);
				json.put("message", "批量删除成功");
			} else {
				json.put("code", 400);
				json.put("message", "无数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "批量删除异常");
		}
		return json;
	}
	
}